import AppPage from '@/views/appPage'
import Ad from 'Comps/Ad'
import Tip from 'Comps/Tip'

import Pano from 'Comps/Pano'
import Notice from 'Comps/Notice/pc'
import Loading from 'Comps/Loading'
// 天气
// import Weather from 'Comps/Weather'
// 弹幕
import Barrage from 'Comps/Barrage'
// 返回总览图
import PendantOvv from 'Comps/Pendant/ovv'
// 返回手绘图
import PendantDraw from 'Comps/Pendant/draw'
// 右侧工具栏
import ToolsBar from 'Comps/ToolsBar'
// 截屏
import ScreenShots from 'Comps/ScreenShots'

// iframe
import SocketIframe from 'Iframe/socket'
import MapIframe from 'Iframe/map'
import LinkIframe from 'Iframe/link'
import ImageIframe from 'Iframe/image'
import ThreeIframe from 'Iframe/three'
import VideoIframe from 'Iframe/video'
import PluginIframe from 'Iframe/plugin'
import RewardIframe from 'Iframe/reward'
import OrderCenter from 'Modal/app/orderCenter'
import StartRemindIframe from 'Iframe/startRemind'
import VideoTransitionIframe from 'Iframe/video-transition'

// 文件目录弹出框
import DirectoryHotspot from 'Modal/DirectoryHotspot'

import { mapGetters } from 'vuex'
import AUTH from '@/utils/auth'
import { getViewport, getPlatform, getIP, isIos, isWxBrowser, setWechatTitle } from '@/utils'
import { initBgMusic, updateBgMusicStatus } from '@/utils/audio'

export default {
  components: {
    AppPage,
    Ad,
    Tip,
    Pano,
    Loading,
    Notice,
    // Weather,
    Barrage,
    PendantOvv,
    PendantDraw,
    ToolsBar,
    ScreenShots,
    SocketIframe,
    MapIframe,
    LinkIframe,
    ImageIframe,
    ThreeIframe,
    VideoIframe,
    PluginIframe,
    RewardIframe,
    OrderCenter,
    StartRemindIframe,
    VideoTransitionIframe,
    DirectoryHotspot,
  },
  data() {
    return {
      isShowLoading: false,
      hasPermision: false,
      isScreenShots: true, // 是否可截图
      isstartsceneShowThree: false,
    }
  },
  computed: {
    ...mapGetters([
      'panoInfo',
      'themeId',
      'sceneList',
      'loadInfo',
      'startPicOrVideo',
      'curSceneMusic',
      'hasPermission',
      'visibleBarrage',
      'menuData',
      'worksData',
      'hotspots',
    ]),
    // 如果是白天黑夜模式，隐藏图片
    getIsShowNight() {
      let isshow = true
      if (this.panoInfo && this.panoInfo.group_scene_list && this.panoInfo.group_scene_list.length > 0) {
        let arr = this.panoInfo.group_scene_list
        arr.map(item1 => {
          if (item1 && item1.scenelist && item1.scenelist.length > 0) {
            item1.scenelist.map(item2 => {
              if(item2.switchscene_id  && item2.switchscene_id !== 0){
                isshow = false
              }
            })
          }
        })
      }
      return isshow
    },
    getHasStartRemind() {
      return this.startPicOrVideo
    },
    getHasLoader() {
      console.log(this.loadInfo)
      return this.loadInfo
    },
    getIsHome() {
      // ishideall=1时隐藏loading
      if (window.location.hash.indexOf('ishideall=1') !== -1) {
        return false;
      }
      return this.$route.name === 'Home'
    },
    getTitle() {
      if (this.title) {
        return this.title
      }
      return ''
    },
    getPermission() {
      const key = AUTH.getCookie('panokey')
      if (key || this.hasPermission) {
        return true
      }
      return false
    },
    getTheme() {
      let themeStr = 'abcdefghijklmn'
      let themeIndex = themeStr.charAt(this.getThemeType)
      themeIndex = themeIndex === 'h' ? 'f' : themeIndex
      return getPlatform() ? `theme-pc-${themeIndex}` : `theme-app-${themeIndex}`
    },
    getThemeType() {
      return this.themeId
    },
    getStartRemind() {
      return this.startPicOrVideo ? true : false
    },
    getWorksList() {
      if (this.worksData && this.worksData.length > 0) {
        return this.worksData
      }
    },
  },
  watch: {
    panoInfo: {
      handler(data) {
        if (data) {
          if (data.isloading === 1 && data.isstartscene === 3) {
            setTimeout(() => {
              this.isShowLoading = true
            }, 3500)
          } else {
            this.isShowLoading = true
          }

          if (data.isstartscene === 3) {
            // 旋转效果处理方式
            let count = 12000
            if (data.isloading === 1) {
              if (data.load_type === 0 && data.loadinfo && data.loadinfo.showtime >= 0) {
                count = data.loadinfo.showtime + 7000
              }
            }
            count = count < 12000 ? 12000 : count
            setTimeout(() => {
              this.isstartsceneShowThree = true
            }, count)
          } else {
            if (data.isloading === 1 && data.load_type === 0) {
              setTimeout(() => {
                this.isstartsceneShowThree = true
              }, ((data.loadinfo && data.loadinfo.showtime) ? data.loadinfo.showtime : 3000))
            } else {
              this.isstartsceneShowThree = true
            }
          }

          document.title = data.pano_name || '全景'
          this.title = data.pano_name
          // weixin browser
          if (isWxBrowser()) {
            setWechatTitle(data.pano_name)
            // 需要关注才能访问作品
            if (data.isfollow == 1) {
              if (!this.$AUTH.getCookie()) {
                this.$AUTH.weixinAuth(this.$route.params.panoId)
              } else {
                this.$store.dispatch('getSubscribeStatus')
                // 获取微信分享与卡卷
                this.getWeixinData()
              }
            } else {
              // 获取微信分享与卡卷
              this.getWeixinData()
            }
          }
        }
      },
      immediate: true,
    },
  },
  mounted() {
    var that = this
    // 监听是否开场提示
    const hasStartPic = () => {
      const re = /mp4|AVI|wma|rmvb|mid|3GP/i
      // 开场不是视频直接播放音乐initBgMusic
      if (!(this.getHasStartRemind && re.test(this.startPicOrVideo))) {
        if (!this.hasVideoInHotspot() || isIos()) {
          initBgMusic()
        } else {
          updateBgMusicStatus()
        }
      }
    }

    // 如果不是微信浏览器，判断是否有Loader
    if (!isWxBrowser()) {
      if (!this.getHasLoader) {
        setTimeout(() => {
          hasStartPic()
        }, 3000)
      } else {
        setTimeout(() => {
          hasStartPic()
        }, this.getHasLoader.showtime + 3000)
      }
    } else {
      setTimeout(() => {
        // 初始化音频
        document.addEventListener('WeixinJSBridgeReady', function() {
          hasStartPic()
          // 隐藏微信网页右上角的按钮
          // WeixinJSBridge.call('hideOptionMenu')
          // WeixinJSBridge.call('showOptionMenu')
          // 显示隐藏微信网页底部的导航栏
          WeixinJSBridge.call('hideToolbar')
          // WeixinJSBridge.call('showToolbar')
          setTimeout(() => {
            that.$vgo.$emit('wxjssdkinit')
          }, 5000)
        }, false)
      }, 800)
    }

    // 截图成功，阻止保存图片时候再次截图
    this.$vgo.on('compose:success', () => {
      this.isScreenShots = false
    })

    // 截图删除，可再次截图
    this.$vgo.on('compose:remove', () => {
      this.isScreenShots = true
    })

    // 发送用户数据
    setTimeout(() => {
      //实例化城市查询类
      var cityData
      var userData = {
        sw: getViewport().VW,
        sh: getViewport().VH,
        ip: getIP(),
        city: '',
        region: '',
        type: getPlatform() ? 1 : 2,
        panoid: this.$store.getters.panoId,
        userid: this.$store.getters.panoInfo
          ? this.$store.getters.panoInfo.user_id
          : '',
      }

      try {
        let citysearch = new AMap.CitySearch()

        //自动获取用户IP，返回当前城市
        citysearch.getLocalCity((status, result) => {
          if (status === 'complete' && result.info === 'OK') {
            cityData = result
            userData.city = cityData ? cityData.city : ''
            userData.region = cityData ? cityData.province : ''
            this.$store.dispatch('sendCustomInfo', userData)
          }
        })
      } catch (e) {
        this.$store.dispatch('sendCustomInfo', userData)
      }
    }, 5000)
  },
  methods: {
    hasVideoInHotspot () {
      let isInit = false
      const id = $VM.$krp.getSceneId()
      if (this.hotspots && this.hotspots[id] && this.hotspots[id].hotspotlist && this.hotspots[id].hotspotlist.length > 0) {
        const hotspotlist = this.hotspots[id].hotspotlist
        hotspotlist.map(item => {
          // 处理音乐重叠
          if (item.diy_id == 0 && item.type_id == 9) {
            isInit = true
          }
        })
      }
      return isInit
    },
    closeLoading() {
      this.isShowLoading = false
    },
    getWeixinData() {
      setTimeout(() => {
        this.$store.dispatch('fetchPanoWxCard')
        this.$store.dispatch('getWXShareData')
      }, 4000)
    },
  },
  
}
